package com.geek.log4jcontrol.remote.impl;

import java.io.File;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import com.geek.log4jcontrol.commons.FinalConstants;
import com.geek.log4jcontrol.core.ConfigHis;
import com.geek.log4jcontrol.core.ConfigHisControler;
import com.geek.log4jcontrol.core.Log4jConfigControler;
import com.geek.log4jcontrol.core.LogControler;
import com.geek.log4jcontrol.remote.Log4jControlRemote;
import com.geek.util.FileContextUtil;

/**
 * @package com.geek.log4jcontrol.rmi.remote.impl
 * @description RMI对外接口的实现类，由RemoteProxyCglib代理执行
 * @author Goliath
 * @createTime 2012-8-2 下午02:35:43
 * @modifyTime
 */
@SuppressWarnings("unchecked")
public class Log4jControlRemoteImpl extends UnicastRemoteObject implements Log4jControlRemote {

	private static final long serialVersionUID = 8278527338760824863L;

	/**
	 * @description log4j的配置调整
	 */
	private Log4jConfigControler log4jConfigControler = Log4jConfigControler.getInstance();
	/**
	 * @description log4j的历史配置操作
	 */
	private ConfigHisControler configHisControler = ConfigHisControler.getInstance();
	/**
	 * @description 日志的操作
	 */
	private LogControler logLoader = LogControler.getInstance();

	public Log4jControlRemoteImpl() throws RemoteException {
		super();
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#getConfig(java.lang.String, java.lang.String)
	 */
	@Override
	public ConfigHis getConfig(String operatePwd, String configCode) throws RemoteException {
		return configHisControler.getConfig(configCode);
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#getConfigList(java.lang.String)
	 */
	@Override
	public List<ConfigHis> getConfigList(String operatePwd) throws RemoteException {
		return configHisControler.getConfigList();
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#getLog(java.lang.String, java.util.Map)
	 */
	@Override
	public File getLog(String operatePwd, Map configMap) throws RemoteException {
		return logLoader.getLog(configMap);
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#initConfig(java.lang.String, java.lang.String)
	 */
	@Override
	public int initConfig(String operatePwd, String configStr) throws RemoteException {
		int operateFlag = log4jConfigControler.initConfig(configStr);
		if (operateFlag == FinalConstants.SUCCESS) {
			log4jConfigControler.setCurrentConfig(configStr);
			configHisControler.makeConfigHis(configStr);
		}
		return operateFlag;
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#initConfig(java.lang.String, java.util.Properties)
	 */
	@Override
	public int initConfig(String operatePwd, Properties properties) throws RemoteException {
		return log4jConfigControler.initConfig(properties);
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#resetConfig(java.lang.String, java.lang.String)
	 */
	@Override
	public int resetConfig(String operatePwd, String configCode) throws RemoteException {
		int operateFlag = log4jConfigControler.resetConfig(configCode);
		if (operateFlag == FinalConstants.SUCCESS) {
			log4jConfigControler.setCurrentConfig(configHisControler.getConfig(configCode).getContext());
			configHisControler.makeConfigHisByConfigCode(configCode);
		}
		return operateFlag;
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#initConfigByPath(java.lang.String, java.lang.String)
	 */
	@Override
	public int initConfigByPath(String operatePwd, String filePath) throws RemoteException {
		int operateFlag = log4jConfigControler.initConfigByPath(filePath);
		if (operateFlag == FinalConstants.SUCCESS) {
			String configStr = FileContextUtil.loadContextString(filePath);
			log4jConfigControler.setCurrentConfig(configStr);
			configHisControler.makeConfigHisByPath(filePath);
		}
		return operateFlag;
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#initConfigByFile(java.lang.String, java.io.File)
	 */
	@Override
	public int initConfigByFile(String operatePwd, File configFile) throws RemoteException {
		int operateFlag = log4jConfigControler.initConfigByFile(configFile);
		if (operateFlag == FinalConstants.SUCCESS) {
			String configStr= FileContextUtil.loadContextString(configFile);
			log4jConfigControler.setCurrentConfig(configStr);
			configHisControler.makeConfigHis(configFile);
		}
		return operateFlag;
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#getCurrentConfig(java.lang.String)
	 */
	@Override
	public String getCurrentConfig(String operatePwd) throws RemoteException {
		return log4jConfigControler.getCurrentConfig();
	}

	/* (non-Javadoc)
	 * @see com.geek.log4jcontrol.remote.Log4jControlRemote#getLogStr(java.lang.String, java.util.Map)
	 */
	@Override
	public String getLogStr(String operatePwd, Map configMap) throws RemoteException {
		return logLoader.getLogStr(configMap);
	}

}
